﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net.Mail;
using System.Configuration;
using System.IO;
using System.Text;

namespace PerformanceAssessment.Models
{
    public class EmailUtil : MailMessage
    {
        public EmailUtil()
        {
        }
        public EmailUtil(string to, string subject, string body)
        {
            this.To.Add(to);
            this.Body = body;
            this.Subject = subject;
            this.IsBodyHtml = true;
        }
        public void Send()
        {
            SmtpClient smtpClient = new SmtpClient();

            //Only sends emails if the configuration flag is set to true. Should be set to false if not in production
            if (bool.Parse(ConfigurationManager.AppSettings["EmailsEnabled"]))
                smtpClient.Send(this);
            else if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["TestEmail"]))
            {
                this.To.Clear();
                this.To.Add(ConfigurationManager.AppSettings["TestEmail"]);
                smtpClient.Send(this);
            }
        }

        public static void SendSubmitAutoEvaluationEmail(Assessment assessment)
        {
        }

        public static void CreateiCalAppointment(string body, string toEmail, string attendeeName, string subject, string loc, 
            string sum, DateTime startDate, DateTime endDate)
        {
            const string c_strTimeFormat = "yyyyMMdd\\THHmmss\\Z";
            string strStartTime = "";
            string strEndTime = "";
            string strTimeStamp = "";
            string strTempStartTime = "";
            string strTempEndTime = "";
            string vCalendarFile = "";
            string location = loc;
            string summary = sum;
            const string VCAL_FILE =
                 "BEGIN:VCALENDAR\n" +
                 "VERSION:1.0\n" +
                 "BEGIN:VEVENT\n" +
                 "DTSTART{0}\n" +
                 "DTEND{1}\n" +
                 "LOCATION;ENCODING=QUOTED-PRINTABLE:{2}\n" +
                 "DESCRIPTION;ENCODING=QUOTED-PRINTABLE:{3}\n" +
                 "SUMMARY;ENCODING=QUOTED-PRINTABLE:{4}\n" +
                 "TRIGGER:-PT15M\n" +
                 "PRIORITY:3\n" +
                 "END:VEVENT\n" +
                 "END:VCALENDAR";
            DateTime dtmStartDate = DateTime.Parse(startDate.ToString("d"));
            DateTime dtmStartTime = DateTime.Parse(startDate.ToString("d") + " " + startDate.TimeOfDay.ToString());
            DateTime dtmEndTime = DateTime.Parse(startDate.ToString("d") + " " + endDate.TimeOfDay.ToString());
            strTempStartTime = string.Format("{0} {1}", dtmStartDate.ToShortDateString(), dtmStartTime.ToLongTimeString());
            strTempEndTime = string.Format("{0} {1}", dtmStartDate.ToShortDateString(), dtmEndTime.ToLongTimeString());
            strTimeStamp = (DateTime.Parse(strTempStartTime)).ToUniversalTime().ToString(c_strTimeFormat);
            strStartTime = string.Format(":{0}", strTimeStamp);
            strEndTime = string.Format(":{0}", (DateTime.Parse(strTempEndTime)).ToUniversalTime().ToString(c_strTimeFormat));
            vCalendarFile =
                 String.Format(VCAL_FILE,
                      strStartTime,
                      strEndTime,
                      location,
                      summary,
                      subject,
                      strTimeStamp,
                      toEmail.Trim(),
                      toEmail.Trim(),
                      attendeeName.Trim(),
                      toEmail.Trim(),
                      toEmail.Trim());

            // Create a memory stream
            MemoryStream memoryStream = new MemoryStream();

            byte[] contentAsBytes = Encoding.UTF8.GetBytes(vCalendarFile);
            memoryStream.Write(contentAsBytes, 0, contentAsBytes.Length);
            // Set the position to the beginning of the stream.
            memoryStream.Seek(0, SeekOrigin.Begin);
            System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Octet);
            System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(memoryStream, ct);
            attach.ContentDisposition.FileName = "invite.ics";

            EmailUtil email = new EmailUtil();
            email.To.Add(toEmail);
            email.Body = body;
            email.Subject = subject;
            email.IsBodyHtml = true;
            // Add the attachment
            email.Attachments.Add(attach);

            // Send Mail via SmtpClient
            email.Send();
            memoryStream.Close();
        }
    }
}